package net.zjitc.controller;

import net.zjitc.pojo.CategorySecond;
import net.zjitc.pojo.Product;
import net.zjitc.service.CategorySecondService;
import net.zjitc.service.ProductService;
import net.zjitc.service.impl.CategorySecondServiceImpl;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.utils.CommonUtils;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/28.
 */
public class AdmianProduct {
    ProductService produceService = new ProductServiceImpl();
    CategorySecondService secondService = new CategorySecondServiceImpl();
    @URLMapping(url = "/AdminProductFindALL")
    public String findAll(HttpServletRequest request, HttpServletResponse response){
        String pageNum = request.getParameter("page");
        int page = Integer.valueOf(pageNum);
        int pageSize = 12;
        PageBean<Product> pageBean = produceService.findPageBean(page,pageSize);
        request.setAttribute("page",pageBean);
        return "/admin/product/list.jsp" ;
    }

    @URLMapping(url = "/AdminProductAddUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response){
        List<CategorySecond> list = secondService.findAll();
        request.setAttribute("list",list);
        return "/admin/product/add.jsp" ;
    }

    @URLMapping(url = "/AdminProductAdd")
    public String add(HttpServletRequest request, HttpServletResponse response){

        DiskFileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload fileUpload = new ServletFileUpload(factory);
        fileUpload.setHeaderEncoding("utf-8");
        Product product = new Product();
        CategorySecond categorySecond = new CategorySecond();
        try {
            List<FileItem> fileItems = fileUpload.parseRequest(request);
            for (FileItem item:fileItems){
                if (item.isFormField()){
                    if ("pname".equals(item.getFieldName())){
                        product.setPname(item.getString("utf-8"));
                    }
                    if ("shopPrice".equals(item.getFieldName())){
                        product.setShop_price(Double.valueOf(item.getString()));
                    }

                    if ("marketPrice".equals(item.getFieldName())){
                        product.setMarket_price(Double.valueOf(item.getString()));
                    }
                    if ("isHot".equals(item.getFieldName())){
                        product.setIs_hot(Integer.valueOf(item.getString()));
                    }
                    if ("pdesc".equals(item.getFieldName())){
                        product.setPdesc(item.getString("utf-8"));
                    }
                    if ("csid".equals(item.getFieldName())){
                        categorySecond.setCsid(Integer.valueOf(item.getString()));
                        product.setCategorySecond(categorySecond);
                    }
                }else {
                    String fileName = item.getName();
                    fileName = CommonUtils.getUUID()+fileName;
                    InputStream inputStream = item.getInputStream();
                    String path = request.getServletContext().getRealPath("/products/5");
                    File dir = new File(path);
                    if (!dir.exists()){
                        dir.mkdirs();
                    }
                    File file = new File(dir,fileName);
                    item.write(file);
                    String imgPath = "products/5/"+fileName;
                    product.setImage(imgPath);
                }
            }

            product.setPdate(new Date());

        } catch (Exception e) {
            e.printStackTrace();
        }
        int count =produceService.insert(product);
        String url = request.getContextPath()+"/AdminProductFindALL?page=1";
        return "Redirect:"+url ;
    }
    @URLMapping(url = "/AdminProductEditUI")
    public String editUI(HttpServletRequest request, HttpServletResponse response){

        return "/admin/product/edit.jsp" ;
    }
    @URLMapping(url = "/AdminProductEdit")
    public String edit(HttpServletRequest request, HttpServletResponse response){
        String url = request.getContextPath()+"/AdminProductFindALL";
        return "Redirect:"+url ;
    }
    @URLMapping(url = "/AdminProductDelete")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String url = request.getContextPath()+"/AdminProductFindALL";
        return "Redirect:"+url ;
    }
}
